<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class penyusutan extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_pengadaan');
        $this->load->model('m_bagian');
        $this->load->model('m_lokasi');
        $this->load->model('m_barang');
        $this->load->model('m_group_barang');
        $this->load->model('m_kategoribarang');
        $this->load->model('m_jenisbarang');
        $this->load->model('m_merk');
        $this->load->model('m_pengajuan_barang');
        $this->load->model('m_penyusutan');
        $this->load->model('m_penyusutan_detail');

        $this->load->model('m_operator_mini');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');

        // load helpers
        $this->smarty->load_javascript("resource/js/jquery/jquery-ui-1.9.2.custom.min.js");
        $this->smarty->load_style("jquery.ui/redmond/jquery-ui-1.8.13.custom.css");
    }

    // welcome administrator
    public function index() {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "inventarisasi/penyusutan/index.html");

        $bulan = Array(
            Array("kode" => "01", "name" => "Januari"),
            Array("kode" => "02", "name" => "Februari"),
            Array("kode" => "03", "name" => "Maret"),
            Array("kode" => "04", "name" => "April"),
            Array("kode" => "05", "name" => "Mei"),
            Array("kode" => "06", "name" => "Juni"),
            Array("kode" => "07", "name" => "Juli"),
            Array("kode" => "08", "name" => "Agustus"),
            Array("kode" => "09", "name" => "September"),
            Array("kode" => "10", "name" => "Oktober"),
            Array("kode" => "11", "name" => "November"),
            Array("kode" => "12", "name" => "Desember")
        );

        $this->smarty->assign("bulan", $bulan);

        $tahun = Array(
            Array("tahun" => "2012"),
            Array("tahun" => "2013"),
            Array("tahun" => "2014"),
            Array("tahun" => "2015"),
            Array("tahun" => "2016"),
            Array("tahun" => "2017"),
            Array("tahun" => "2018"),
            Array("tahun" => "2019"),
            Array("tahun" => "2020")
        );
        $this->smarty->assign("tahun", $tahun);

        $thn_sekarang = date('Y');
        $this->smarty->assign("thn_sekarang", $thn_sekarang);

        $bln_sekarang = date('m');
        $this->smarty->assign("bln_sekarang", $bln_sekarang);


        //show history        
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("inventarisasi/penyusutan/index/");
        $config['total_rows'] = $this->m_penyusutan->get_count_all_penyusutan();
        $config['uri_segment'] = 4;
        $config['per_page'] = 25;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array(($start - 1), $config['per_page']);
        $result = $this->m_penyusutan->get_all_penyusutan_pagination($params);
        $this->smarty->assign("rs_id", $result);

        //bulan        
        $bulan_arr = array("01" => "Januari",
            "02" => "Februari",
            "03" => "Maret",
            "04" => "April",
            "05" => "Mei",
            "06" => "Juni",
            "07" => "Juli",
            "08" => "Agustus",
            "09" => "September",
            "10" => "Oktober",
            "11" => "November",
            "12" => "Desember");
        $this->smarty->assign("bulan_arr", $bulan_arr);


        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        // output
        parent::display();
    }

    public function process() {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "inventarisasi/penyusutan/index.html");

        $this->tnotification->set_rules('penyusutan_tgl', 'Periode', 'trim|required');
        if ($this->tnotification->run() !== FALSE) {
            //proses
            $rs_barang = $this->m_barang->get_all_barang_penyusutan();
            $tgl_sekarang = $this->get_hitung_masa_penyusutan($this->input->post('penyusutan_tgl'));



            //insert into penyusutan
            $params = array($tgl_sekarang, $this->input->post('penyusutan_keterangan'), $this->com_user['user_id']);
            $this->m_penyusutan->insert($params);

            //get id penyusutan terakir
            $last_penyusutan_id = $this->m_penyusutan->get_last_penyusutan_id();

            // print_r($rs_barang);

            foreach ($rs_barang as $row) {

                //cek pernah disusutkan atau belum
                //selisih bulan
                $rs_pengadaan = $this->m_pengadaan->get_pengadaan_by_pengadaan_id($row['pengadaan_id']);
                $tgl_dist = $rs_pengadaan['pengadaan_tgl_dist'];
                //echo $tgl_dist;

                $tgl_terakhir = $tgl_dist;
                if ($tgl_terakhir == '') {
                    $tgl_terakhir = $row['tanggal'];
                    //echo $tgl_terakhir;
                }
              

                $date_diff = strtotime($tgl_sekarang) - strtotime($tgl_terakhir);
                $selisih = floor(($date_diff) / 2628000);

                //echo $tgl_sekarang . " - " . $tgl_terakhir . " -> " . $selisih . " ";
                //harga awal
                $harga = $row['barang_harga_beli'];
                $umur_ekonomis = $row['barang_umurekonomis'] * 12;
                $pengurangan_per_bulan = ceil($harga / $umur_ekonomis);
                $pengurangan_total = ($pengurangan_per_bulan * $selisih);

                $nilai_akhir = $harga - $pengurangan_total;
                if ($nilai_akhir < 0) {
                    $nilai_akhir = 0;
                }
                echo "tgl_terakhir          " . $tgl_terakhir;
                echo "<br />";
                echo "harga                 " . $harga;
                echo "<br />";
                echo "umur_ekonomis         " . $umur_ekonomis;
                echo "<br />";
                echo "selisih               " . $selisih;
                echo "<br />";
                echo "pengurangan_per_bulan " . $pengurangan_total;
                echo "<br />";
                echo "nilai_akhir           " . $nilai_akhir;

                //update hari di barang
                $this->m_barang->edit_barang_tglpenyusutan_tglpenyusutanbarang_nilai_buku(array($tgl_sekarang, $nilai_akhir, $row['barang_id']));

                //data
                $params = array($last_penyusutan_id,
                    $row['barang_id'],
                    $tgl_terakhir,
                    $tgl_sekarang,
                    $harga,
                    $nilai_akhir,
                    $this->com_user['user_id']
                );

                //print_r($params);
                //exit();
                //insert into penyusutan detail
                //$result = $this->m_penyusutan_detail->get_penyusutan_by_penyusutan_id($last_penyusutan_id);
               // if (empty($result)) {
                    $this->m_penyusutan_detail->insert($params);
              //  }
            }
            $this->tnotification->sent_notification("success", "Penyusutan berhasil diproses");
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        redirect('inventarisasi/penyusutan');
    }

    //detail penyusutan
    public function detail($id) {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "inventarisasi/penyusutan/detail.html");

        //show history        
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("inventarisasi/penyusutan/detail/" . $id . "/");
        $config['total_rows'] = $this->m_penyusutan_detail->get_count_all_penyusutan_detail_by_penyusutandet_id($id);
        $config['uri_segment'] = 5;
        $config['per_page'] = 25;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(5, 0) + 1;
        $end = $this->uri->segment(5, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($id, ($start - 1), $config['per_page']);
        $result = $this->m_penyusutan_detail->get_all_penyusutan_detail_by_penyusutandet_id_pagination($params);
        $this->smarty->assign("rs_id", $result);

        //print_r($result);
        //bulan        
        $bulan_arr = array("01" => "Januari",
            "02" => "Februari",
            "03" => "Maret",
            "04" => "April",
            "05" => "Mei",
            "06" => "Juni",
            "07" => "Juli",
            "08" => "Agustus",
            "09" => "September",
            "10" => "Oktober",
            "11" => "November",
            "12" => "Desember");
        $this->smarty->assign("bulan_arr", $bulan_arr);


        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        // output
        parent::display();
    }

    // hitung masa berlaku
    public function hitung_masa_penyusutan() {
        $tanggal = $this->input->post('tanggal');
        $expired_date = $this->m_penyusutan->hitung_tanggal_penyusutan($tanggal);
        if ($expired_date) {
            echo $this->datetimemanipulation->get_full_date($expired_date);
        } else {
            echo "";
        }
    }

    // hitung masa berlaku
    public function get_hitung_masa_penyusutan($tanggal) {
        $expired_date = $this->m_penyusutan->hitung_tanggal_penyusutan($tanggal);
        if ($expired_date) {
            return $expired_date;
        } else {
            echo "";
        }
    }

}
